package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import mappers.MemberMapper;
import domains.Member;

public class EditMemberGUI {
	private JFrame editMemberFrame;
	private JPanel btnsPanel, panel;
	private JButton editBtn, cancelBtn;
	private JLabel fNameLbl, lNameLbl, addressLbl, zipCodeLbl, cityLbl, emailLbl,
					phoneNoLbl;
	private JTextField fNameTF, lNameTF, addressTF, zipCodeTF, cityTF, emailTF,
	phoneNoTF; ;
	private GridLayout layout;

	public EditMemberGUI(final ShowMembersGUI gui) {
		
		layout = new GridLayout(0,2,1,0);
		
		panel = new JPanel();
		panel.setLayout(layout);
		layout.setVgap(0);
		
		fNameLbl = new JLabel("Fornavn: ");
		fNameTF = new JTextField(15);
		
		lNameLbl = new JLabel("Efternavn: ");
		lNameTF = new JTextField(15);
		
		addressLbl = new JLabel("Adresse: ");
		addressTF = new JTextField(15);	

		zipCodeLbl = new JLabel("Postnummer: ");
		zipCodeTF = new JTextField(15);
		
		cityLbl = new JLabel("By: ");
		cityTF = new JTextField(15);
		
		emailLbl = new JLabel("Email: ");
		emailTF = new JTextField(15);
		
		phoneNoLbl = new JLabel("Telefonnr: ");
		phoneNoTF = new JTextField(15);
			
		panel.add(fNameLbl);
		panel.add(fNameTF);
		panel.add(lNameLbl);
		panel.add(lNameTF);
		panel.add(addressLbl);
		panel.add(addressTF);
		panel.add(zipCodeLbl);
		panel.add(zipCodeTF);
		panel.add(cityLbl);
		panel.add(cityTF);
		panel.add(emailLbl);
		panel.add(emailTF);
		panel.add(phoneNoLbl);
		panel.add(phoneNoTF);
		
		
		//------------
		
		btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		editBtn = new JButton("Redigér");
		editBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {				
				int zipCode = Integer.parseInt(zipCodeTF.getText());
				Member m = new Member(fNameTF.getText(), lNameTF.getText(), 
						addressTF.getText(), zipCode, cityTF.getText(),
						emailTF.getText(), phoneNoTF.getText());
				MemberMapper mMapper = new MemberMapper();
				int i = gui.getIndex();
				mMapper.editMember(m, i);
				
				editMemberFrame.setVisible(false);
				
				List<Member> data = mMapper.selectAllMembers();
				MyTableModel myTableModel = new MyTableModel(data);
								
				gui.getTable().setModel(myTableModel);
				int row = gui.getTable().getSelectedRow();
				myTableModel.tableUpdate(row, m);
			}	
		});
	
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				editMemberFrame.setVisible(false);
			}	
		});
		
		btnsPanel.add(editBtn);
		btnsPanel.add(cancelBtn);
		
	}
	
	public void showGUI() {
		editMemberFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = editMemberFrame.getSize().width;
		int h = editMemberFrame.getSize().height;
		int x = (dim.width-w)/4;
		int y = (dim.height-h)/4;
		editMemberFrame.setLocation(x, y);
		editMemberFrame.setTitle("Redigér Medlem");
		editMemberFrame.setLayout(new BorderLayout());
		editMemberFrame.setSize(400,300);
		editMemberFrame.add(panel, BorderLayout.CENTER);
		editMemberFrame.add(btnsPanel, BorderLayout.SOUTH);
		//editMemberFrame.pack();
		editMemberFrame.setVisible(true);
	}

	public JTextField getfNameTF() {
		return fNameTF;
	}

	public void setfNameTF(JTextField fNameTF) {
		this.fNameTF = fNameTF;
	}

	public JTextField getlNameTF() {
		return lNameTF;
	}

	public void setlNameTF(JTextField lNameTF) {
		this.lNameTF = lNameTF;
	}

	public JTextField getAddressTF() {
		return addressTF;
	}

	public void setAddressTF(JTextField addressTF) {
		this.addressTF = addressTF;
	}

	public JTextField getZipCodeTF() {
		return zipCodeTF;
	}

	public void setZipCodeTF(JTextField zipCodeTF) {
		this.zipCodeTF = zipCodeTF;
	}

	public JTextField getCityTF() {
		return cityTF;
	}

	public void setCityTF(JTextField cityTF) {
		this.cityTF = cityTF;
	}

	public JTextField getEmailTF() {
		return emailTF;
	}

	public void setEmailTF(JTextField emailTF) {
		this.emailTF = emailTF;
	}

	public JTextField getPhoneNoTF() {
		return phoneNoTF;
	}

	public void setPhoneNoTF(JTextField phoneNoTF) {
		this.phoneNoTF = phoneNoTF;
	}
}
